<template>
	<div class="system-group-container">
		<el-row :gutter="15">
			<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="mb15">
				<el-card shadow="hover">
					<el-form :inline="true" :model="search">
						<el-form-item>
							<el-input v-model="search.groupName" placeholder="请输入群组名称" prefix-icon="el-icon-search"></el-input>
						</el-form-item>
						<el-form-item>
							<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
							<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
						</el-form-item>
						<el-form-item>
							<el-button v-auth="'system:group:add'" type="primary" icon="el-icon-document-add" @click="onOpenAddGroup()">建群</el-button>
							<el-button v-auth="'system:group:edit'" type="primary" icon="el-icon-edit" @click="onOpenEditGroup()" :disabled="tableData.selections.length != 1">修改</el-button>
							<el-button v-auth="'system:group:remove'" type="primary" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">解散</el-button>
						</el-form-item>
					</el-form>
					<el-table ref="tableGroupRef" :data="tableData.data" @selection-change="onTableSelectionChange" @row-click="onTableRowClick" style="width: 100%" row-key="id">
						<el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
						<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
						<el-table-column label="群组名称" prop="groupName" show-overflow-tooltip></el-table-column>
						<el-table-column label="群组编码" prop="groupCode" width="100" show-overflow-tooltip></el-table-column>
						<el-table-column label="状态" show-overflow-tooltip width="70">
							<template #default="scope">
								<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
								<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
								<span v-else class="color-danger">解散</span>
							</template>
						</el-table-column>
						<el-table-column label="群组排序" prop="orderNum" width="80" align="center" show-overflow-tooltip></el-table-column>
						<!-- <el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column> -->
						<!-- <el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column> -->
						<el-table-column label="操作" show-overflow-tooltip width="125"
							v-auths="['system:group:edit', 'system:group:remove']">
							<template #default="scope">
								<el-button v-auth="'system:group:edit'" type="primary" link @click="onOpenEditGroup(scope.row)">修改</el-button>
								<el-button v-auth="'system:group:remove'" type="primary" link @click="onTabelRowDel(scope.row)">解散</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination
						@size-change="onPageSizeChange"
						@current-change="onPageCurrentChange"
						class="mt15"
						:pager-count="5"
						:page-sizes="[10, 20, 30, 40, 50, 60]"
						v-model:current-page="search.pageNum"
						background
						v-model:page-size="search.pageSize"
						layout="total, sizes, prev, pager, next, jumper"
						:total="tableData.total"
					>
					</el-pagination>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="mb15">
				<UserGroup ref="userGroupRef" :model="tableData.group"></UserGroup>
			</el-col>
		</el-row>
		<AddGroup ref="addGroupRef" @submit="onTableSearch"/>
		<EditGroup ref="editGroupRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddGroup from '/@/views/system/group/addGroup.vue';
import EditGroup from '/@/views/system/group/editGroup.vue';
import UserGroup from '/@/views/system/group/userGroup.vue';
import { getGroupList, delGroup } from '/@/api/system/group/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
	name: 'group',
	components: { AddGroup, EditGroup, UserGroup },
	setup() {
		const tableGroupRef = ref();
		const userGroupRef = ref();
		const addGroupRef = ref();
		const editGroupRef = ref();
		const state = reactive({
			search:{
				groupName: undefined,
				groupCode: undefined,
				startTime: undefined,
				endTime: undefined,
				orderBy: 'orderNum',
				isAsc: 'asc',
				pageNum: 1,
				pageSize: 10
			},
			dateData: {
				value: [],
				shortcuts: getDateRangeShortcuts()
			},
			tableData: {
				group: {},
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getGroupList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
					tableGroupRef.value.clearSelection();
				}
			});
		}

		//查询
		const onTableSearch = () => {
			if(state.dateData.value.length == 2){
				state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
				state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
			}
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.dateData.value = [];
			state.search.groupName = undefined;
			state.search.groupCode = undefined;
			state.search.startTime = undefined;
			state.search.endTime = undefined;
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 打开新增群组弹窗
		const onOpenAddGroup = (row: object) => {
			addGroupRef.value.openDialog(row);
		};
		// 打开编辑群组弹窗
		const onOpenEditGroup = (row: object) => {
			row = row || state.tableData.selections[0];
			editGroupRef.value.openDialog(row);
		};
		// 解散选中行
		const onTabelRowsDel = () => {
			const ids = state.tableData.selections.map(item => item.id);
			if(ids && ids.length > 0){
				ElMessageBox.confirm('确定解散选中的群组信息吗？', '提示', {
					confirmButtonText: '解散',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					delGroup(ids).then((res) => {
						if(res.code === 0){
							ElMessage.success('解散成功');
							loadTableData();
							state.tableData.group = {};
						}
					});
				}).catch(() => {});
			}
		};
		// 解散当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定解散该条【' + row.groupName + '】群组信息吗？', '提示', {
				confirmButtonText: '解散',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delGroup([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('解散成功');
						loadTableData();
						state.tableData.group = {};
					}
				});
			}).catch(() => {});
		};
		const onTableRowClick = (row: object) => {
			state.tableData.group = row;
		}
		// 页面加载时
		onMounted(() => {
			loadTableData();
		});
		return {
			tableGroupRef,
			userGroupRef,
			addGroupRef,
			editGroupRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onOpenAddGroup,
			onOpenEditGroup,
			onTabelRowsDel,
			onTabelRowDel,
			onTableRowClick,
			...toRefs(state),
		};
	},
};
</script>